package com.atguigu.juc.date108;

import java.util.concurrent.CountDownLatch;

/**
 * @Author: shenhao
 * @CreatTime: 2024/10/8 18:49
 * @Version: 1.0
 * volatile不是原子性的
 */
public class VolatileNotAtomicDemo {
    public static final int NUMBER200=200;
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch=new CountDownLatch(NUMBER200);
        MyNumber myNumber = new MyNumber();
        for (int result = 0; result < 200; result++) {
            new Thread(()->{
                for (int i = 0; i < NUMBER200; i++) {
                    myNumber.add();
                }
                countDownLatch.countDown();
            },String.valueOf(result)).start();
        }
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName()+"\t"+myNumber.number);//main	39893 表示不是原子性的
    }
}
class MyNumber{
    volatile int number=0;

    public void add(){
        number++;//Non-atomic operation on volatile field 'number'
    }
}
